package com.younion.business.impl;

import java.io.File;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.CollectionUtils;

import cpcns.component.common.base.BaseException;
import cpcns.jhsks.util.MapCollectionUtils;

import com.younion.business.IFavourableEntityService;
import com.younion.common.constants.SysConstants;
import com.younion.common.paginator.Paginator;
import com.younion.common.util.ImageFileUtil;
import com.younion.dao.FavourableEntityDAO;
import com.younion.vo.FavourableEntityVO;

 
public class FavourableEntityServiceImpl implements IFavourableEntityService {

	protected static final Log logger = LogFactory.getLog(FavourableEntityServiceImpl.class);

	private FavourableEntityDAO favourableEntityDAO;
	public Collection getAll() throws Exception {
		return favourableEntityDAO.selectAll();
	}

	public FavourableEntityVO getById(String id) throws Exception {
		if (StringUtils.isEmpty(id) || Integer.parseInt(id)<1)
			return null;
		try {
			Map paramMap = new HashMap();
			paramMap.put("id", new Integer(id));
			Object obj = favourableEntityDAO.selectById(paramMap);
			return obj==null?null:(FavourableEntityVO)obj;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(FavourableEntityServiceImpl.class.getName()+" >> public FavourableVO get(String id) error occur:",e);
		}
	}
	public Collection getByMap(Map paramMap) throws BaseException {
		if(CollectionUtils.isEmpty(paramMap)){
			return null;
		}
		try{
			return favourableEntityDAO.selectByMap(paramMap);
		}catch(Exception e){
			logger.error(e.getMessage(), e);
			throw new BaseException(FavourableEntityServiceImpl.class.getName()+" >> public Collection getByMap(Map map) error occur:",e);
		}
	}

	public int getCountByMap(Map paramMap) throws BaseException {
		try {
			return favourableEntityDAO.selectCountByMap(paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(FavourableEntityServiceImpl.class.getName()
					+ " >> public int getCountByMap(Map map) error occur:", e);
		}
	}

	public List get(Map paramMap, Paginator paginator) throws Exception {
		if(MapCollectionUtils.isEmpty(paramMap)){
			return null;
		}
		try {
			Map map = new HashMap();
			map.put("paramMap", paramMap);
			return favourableEntityDAO.selectForPage(map, paginator);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(FavourableEntityServiceImpl.class.getName()+" >> public Page (Map paramMap, Paginator paginator) error occur:",e);
		}
	}

	public FavourableEntityVO save(FavourableEntityVO voObject) throws Exception {
		if (voObject == null)
			return null;
		try {
			Object id = null;
			id = voObject.getId();
			Map paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils.describe(voObject));
			if (id != null) {
				favourableEntityDAO.updateByMap(paramMap);
				return voObject;
			} else {
				return (FavourableEntityVO)favourableEntityDAO.insertByVo(voObject);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(FavourableEntityServiceImpl.class.getName()+" >> public FavourableVO save(FavourableVO voObject) error occur:",e);
		}
	}

	public Collection save(Collection voList) throws Exception {
		if (CollectionUtils.isEmpty(voList))
			return null;
		try {
			Iterator it = voList.iterator();
			while (it.hasNext()) {
				FavourableEntityVO data = (FavourableEntityVO) it.next();
				data = save(data);
			}
			return voList;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(FavourableEntityServiceImpl.class.getName()+" >> public Collection save(Collection voList) error occur:",e);
		}
	}

	public boolean remove(List ids) throws Exception {
		if (CollectionUtils.isEmpty(ids))
			return false;
		try{
			favourableEntityDAO.deleteByIds(ids);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(FavourableEntityServiceImpl.class.getName()+" >> public boolean remove(List ids) error occur:",e);
		}
	}

	public boolean removeByMap(Map paramMap) throws Exception {
		if (CollectionUtils.isEmpty(paramMap))
			return false;
		try {
			favourableEntityDAO.deleteByMap(paramMap);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(FavourableEntityServiceImpl.class.getName()+" >> public boolean removeByMap(Map paramMap) error occur:",e);
		}
	}
	
	public int saveReturnKey(FavourableEntityVO voObject) throws Exception {
		if (voObject == null)
			return 0;
		try {
			Object id = null;
			id = voObject.getId();
			Map paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils.describe(voObject));
			if (id != null) {
					favourableEntityDAO.updateByMap(paramMap);
					return Integer.parseInt(id.toString());
				}else{
					return favourableEntityDAO.saveReturnKey(voObject);
				}
			} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					FavourableEntityServiceImpl.class.getName()
							+ " >> public int saveReturnKey(FavourableEntityVO voObject) error occur:",
					e);
		}
	}
	
	public boolean modifyAmount(String id) throws Exception{
		if (StringUtils.isEmpty(id) || Integer.parseInt(id)<1)
			return false;
		try{
		Map paramMap = new HashMap();
		paramMap.put("id", new Integer(id));
		favourableEntityDAO.minusAmount(paramMap);
		return true;
		}catch(Exception e){
			logger.error(e.getMessage(), e);
			throw new BaseException(FavourableEntityServiceImpl.class.getName()+" >> public boolean minusAmount(String id) error occur:",e);
		}
	}
	public String selectAppliedNumByUserToken(Map paramMap) throws BaseException {
		try {
			return (favourableEntityDAO.selectAppliedNumByUserToken(paramMap)==null)?"":
				favourableEntityDAO.selectAppliedNumByUserToken(paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(FavourableEntityServiceImpl.class.getName()
					+ " >> public int selectAppliedNumByUserToken(Map map) error occur:", e);
		}
	}
	
	/**
	 * @param service
	 */
	public void setFavourableEntityDAO(FavourableEntityDAO favourableEntityDAO) {
		this.favourableEntityDAO = favourableEntityDAO;
	}
	
	public boolean updateFavourAmountByMap(Map paramMap) throws Exception {
		if (CollectionUtils.isEmpty(paramMap))
			return false;
		try {
			favourableEntityDAO.updateFavourAmountByMap(paramMap);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					CardCountInitServiceImpl.class.getName()
							+ " >> public boolean updateFavourAmountByMap(Map paramMap) error occur:",
					e);
		}
	}
	
	public int getAppliedCount(Map paramMap) throws BaseException {
		try {
			return favourableEntityDAO.selectAppliedCount(paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(FavourableEntityServiceImpl.class.getName()
					+ " >> public int getAppliedCount(Map paramMap) error occur:", e);
		}
	}
	
	public Collection getApiFavourableEntity(Map paramMap) throws BaseException {
		if(CollectionUtils.isEmpty(paramMap)){
			return null;
		}
		try{
			return favourableEntityDAO.selectApiFavourableEntity(paramMap);
		}catch(Exception e){
			logger.error(e.getMessage(), e);
			throw new BaseException(FavourableEntityServiceImpl.class.getName()+" >> public Collection getApiFavourableEntity(Map map) error occur:",e);
		}
	}
	
	public boolean deleteImgByCustomerIds(List<String> customerIds , String path) throws BaseException{
		if(CollectionUtils.isEmpty(customerIds)){
			return false;
		}
		try{
			Map map = new HashMap();
			for(String customerId : customerIds){
				map.put("customerId", customerId);
				List<FavourableEntityVO> favList = (List<FavourableEntityVO>) getByMap(map);
				for(FavourableEntityVO fav : favList){
					File directory = new File(path + File.separator
							+ fav.getId());
					String fileNames = fav.getNonactivatedPic()+","+fav.getActivatedPic();
					ImageFileUtil.deleteImage(directory, fileNames, true);	
				}
				map.clear();
			}
			return true;
		}catch(Exception e){
			logger.error(e.getMessage(), e);
			throw new BaseException(FavourableEntityServiceImpl.class.getName()+" >> public Collection deleteImgByCustomerIds(List customerIds , String path) error occur:",e);
			
		}
	}
}